package com.bw.user_module.user;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.bw.common.entity.LiveDataEntity;
import com.bw.common.entity.RoomUserEntity;
import com.bw.common.eventbus.EventBusEntity;
import com.bw.common.mvvm.view.BaseFragment;
import com.bw.common.utils.ToastShowUtils;
import com.bw.glide_sdk.LoadImage;
import com.bw.user_module.databinding.FragmentUserBinding;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class UserFragment extends BaseFragment<FragmentUserBinding,UserViewModel> {

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        return FragmentUserBinding.inflate(inflater);
    }

    @Override
    public void initView() {
        v.userAtrist.setOnClickListener(v->{
            ARouter.getInstance().build("/login/loginactivity").navigation();
        });
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initData() {

    }


    @Override
    public void handleMessage(LiveDataEntity entity) {
        switch (entity.action) {
            case START_VIEW_MODEL:
                RoomUserEntity roomUserEntity = (RoomUserEntity) entity.data.get(LiveDataEntity.LiveDataType.ENTITY);
                v.userAtrist.setText(roomUserEntity.data.data.name);
                v.userDesc.setText(roomUserEntity.data.data.description);
                LoadImage.LoadCirImg(roomUserEntity.data.data.avatar,v.userArtistImg);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(EventBusEntity eventBusEntity){
        if (eventBusEntity.action.equals("refresh")){
            RoomUserEntity roomUserEntity = (RoomUserEntity) eventBusEntity.data.get("RoomEntity");
            v.userAtrist.setText(roomUserEntity.data.data.name);
            v.userDesc.setText(roomUserEntity.data.data.description);
            LoadImage.LoadCirImg(roomUserEntity.data.data.avatar,v.userArtistImg);
        }
    }

}
